//
//  OMTAlbumHeader.m
//  OMTImagePicker
//
//  Created by yizhaorong on 2018/1/12.
//

#import "OMTAlbumHeader.h"
#import "OMTImagePickerController.h"

@interface OMTAlbumHeader ()

@property (nonatomic, strong) UIControl *contentView;

@property (nonatomic, strong) UIImageView *backgroundView;

@property (nonatomic, strong) UILabel *messageLabel;

@property (nonatomic, strong) UIButton *recordButton;

@end

@implementation OMTAlbumHeader

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    
    _contentView = [UIControl new];
    [_contentView addTarget:self action:@selector(startRecordAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_contentView];
    _backgroundView = [UIImageView new];
    _backgroundView.clipsToBounds = YES;
    _backgroundView.contentMode = UIViewContentModeScaleAspectFill;
    [_contentView addSubview:_backgroundView];
    
    _messageLabel = [UILabel new];
    _messageLabel.font = [UIFont boldSystemFontOfSize:16];
    _messageLabel.textColor = [UIColor whiteColor];
    [_backgroundView addSubview:_messageLabel];
    
    _recordButton = [UIButton new];
    _recordButton.userInteractionEnabled = NO;
    [_backgroundView addSubview:_recordButton];
    
    if (omt_album_is_rtl_language()) {
        self.contentView.transform = CGAffineTransformMakeScale(-1, 1);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.contentView.frame = self.bounds;
    CGRect backgroundFrame = self.contentView.bounds;
    backgroundFrame.size.height = CGRectGetHeight(self.frame) - 3;
    self.backgroundView.frame = backgroundFrame;
    CGFloat messageW = CGRectGetWidth(self.messageLabel.frame);
    CGFloat messageH = CGRectGetHeight(self.messageLabel.frame);
    CGFloat recordW = CGRectGetWidth(self.recordButton.frame);
    CGFloat recordH = CGRectGetHeight(self.recordButton.frame);
    CGFloat margin = 11;
    CGFloat totalW = messageW + recordW + margin;
    CGFloat messageX = (CGRectGetWidth(backgroundFrame) - totalW) / 2;
    CGFloat messageY = (CGRectGetHeight(backgroundFrame) - messageH) / 2;
    CGRect messageFrame = CGRectMake(messageX, messageY, messageW, messageH);
    self.messageLabel.frame = messageFrame;
    
    CGFloat recordX = CGRectGetMaxX(messageFrame) + margin;
    CGFloat recordY = (CGRectGetHeight(backgroundFrame) - recordH) / 2;
    CGRect recordFrame = CGRectMake(recordX, recordY, recordW, recordH);
    self.recordButton.frame = recordFrame;
}

- (void)startRecordAction:(id)sender {
    if (self.delegateController.imagePickerControllerStartRecordVideoHandle) {
        self.delegateController.imagePickerControllerStartRecordVideoHandle(self.delegateController);
    }
}

- (void)setDelegateController:(OMTImagePickerController *)delegateController {
    _delegateController = delegateController;
    UIImage *(^AlbumImageNamed)(NSString *) = [delegateController.builder.imageWithNamed copy];
    NSString *(^Album18N)(NSString *) = [delegateController.builder.localizedString copy];
    _backgroundView.image = AlbumImageNamed(@"omt_record_bg");
    _messageLabel.text = Album18N(@"omt_album_record_message");
    [_messageLabel sizeToFit];
    [_recordButton setImage:AlbumImageNamed(@"omt_record_icon") forState:UIControlStateNormal];
    [_recordButton sizeToFit];
}

@end
